﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mozlite.Common;
using Mozlite.Common.Configuration;

namespace Mozlite.Web.UI.Areas.ControlPanel.Controllers
{
    public class DialogController : ControlPanelBaseController
    {
        public ActionResult UrlView()
        {
            Route route = Routes.GetRoute(Request.QueryString["id"].ToInt32());
            if (route == null)
            {
                route = new Route();
                route.AreaName = Request.QueryString["area"];
            }
            return View(route);
        }

        [HttpPost]
        public ActionResult UrlView(Route route, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                route.RouteID = Request.QueryString["id"].ToInt32();
                if (route.IsNullOrEmpty)
                    return View(route);
                route.Key = "db." + route.Key;
                if (Routes.SaveRoute(route))
                    SetResourceStatus("Route.SaveSuccess");
            }
            return View(route);
        }

        public ActionResult Reset()
        {
            Caches.Clear();
            try
            {
                Globals.ResetApplication();
                SetResourceStatus("ApplicationReset.Success");
            }
            catch {
                SetResourceStatus("ApplicationReset.Error");
            }
            return View();
        }

        public ActionResult LogView()
        {
            return View(Logs.GetLog(Request.QueryString["id"].ToInt32()));
        }

        public ActionResult HostView()
        {
            var site = SiteDomains.GetSite(Request.QueryString["id"].ToInt32());
            if (site == null)
                site = new SiteDomain();
            return View(site);
        }

        [HttpPost]
        public ActionResult HostView(SiteDomain model, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                if (string.IsNullOrEmpty(model.Domain))
                    return View(model);
                model.SiteID = Request.QueryString["id"].ToInt32();
                var status = SiteDomains.SaveSite(model);
                SetResourceStatus(status, status != CreateSiteStatus.Success && status != CreateSiteStatus.Updated);
                return View();
            }
            return View(model);
        }

        public ActionResult RoleView()
        {
            return View(Roles.GetRole(Request.QueryString["id"].ToInt32(), true));
        }

        [HttpPost]
        public ActionResult RoleView(Role model)
        {
            if (ModelState.IsValid)
            {
                model.RoleID = Request.QueryString["id"].ToInt32();
                if (Roles.SaveRole(model))
                {
                    SetResourceStatus("Role.Save.Success");
                    return View();
                }
            }
            return View(model);
        }

        public ActionResult CategoryView()
        {
            var category = Categories.GetCategoryFromCache(this.Get<int>("id"));
            if (category == null)
                category = new Category();
            return View(category);
        }

        [HttpPost]
        public ActionResult CategoryView(Category model)
        {
            if (ModelState.IsValid)
            {
                model.CategoryID = this.Get<int>("id");
                model.SettingsID = Settings.SettingsID;
                var status = Categories.SaveCategory(model);
                SetResourceStatus(status, status != CreateCategoryStatus.Created && status != CreateCategoryStatus.Updated);
            }
            return View(model);
        }

        public ActionResult ForumPermissionSchemaView() {
            var schemaID = this.Get<Guid>("id", Guid.Empty);
            if (schemaID != Guid.Empty)
                return View(Permissions.GetPermissionSchema(schemaID));
            return View(new PermissionSchema());
        }

        [HttpPost]
        public ActionResult ForumPermissionSchemaView(PermissionSchema model, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                model.PermissionType = typeof(ForumPermission).GetInstanceType();
                model.SettingsID = Settings.SettingsID;
                var schemaID = this.Get<Guid>("id", Guid.Empty);
                if (schemaID != Guid.Empty)
                {
                    if (Permissions.IsPermissionSchemaNameValid<ForumPermission>(model.SchemaName, schemaID))
                    {
                        model.SchemaID = schemaID;
                        if (Permissions.UpdatePermissionSchema(model))
                            SetResourceStatus("PermissionSchema.Update.Success");
                        else
                            SetResourceStatus("PermissionSchema.Update.Error");
                    }
                    else
                    {
                        SetResourceStatus("PermissionSchema.DuplicateSchemaName");
                    }
                }
                else
                {
                    if (Permissions.IsPermissionSchemaNameValid<ForumPermission>(model.SchemaName, Guid.Empty))
                    {
                        model.SchemaID = Guid.NewGuid();
                        if (Permissions.CreatePermissionSchema(model))
                            SetResourceStatus("PermissionSchema.Create.Success");
                        else
                            SetResourceStatus("PermissionSchema.Create.Error");
                    }
                    else
                    {
                        SetResourceStatus("PermissionSchema.DuplicateSchemaName");
                    }
                }
            }
            return View(model);
        }

        public ActionResult NavigatorView() {
            ViewBag.Theme = RouteData.Get<string>("key");
            var id = this.Get<int>("id");
            if (id > 0)
                return View(Navigators.GetNavigator(id));
            return View(new Navigator(ViewBag.Theme));
        }

        [HttpPost]
        public ActionResult NavigatorView(Navigator model)
        {
            var key = RouteData.Get<string>("key");
            ViewBag.Theme = key;
            if (ModelState.IsValid)
            {
                var theme = ThemeConfiguration.Themes[key];
                if (theme == null)
                {
                    SetResourceStatus("Themes.NotFound", true);
                    return View(model);
                }
                model.NavigatorID = this.Get<int>("id");
                model.ThemeID = theme.GuidKey;
                model.SettingsID = Settings.SettingsID;
                if (Navigators.SaveNavigator(model))
                    SetResourceStatus("Navigator.SaveSuccess");
            }
            return View(model);
        }
    }
}
